package com.shujia.kafka

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import java.util.Properties
import scala.io.{BufferedSource, Source}

object Demo2StudentToKafka {
  def main(args: Array[String]): Unit = {
    val properties = new Properties()
    //kafka 集群地址
    properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092")

    //key和value的序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")

    //创建生产者，本质是创建和kafka的连接
    val producer = new KafkaProducer[String, String](properties)

    //读取学生表，循环将数据写入kafka
    val source: BufferedSource = Source.fromFile("flink/data/students.txt")

    //获取所有行
    val students: Iterator[String] = source.getLines()

    //循环将数据写入kafka
    for (student <- students) {

      //当topic不存在是会自动创建一个分区为1副本为1的topic
      val record = new ProducerRecord[String, String]("student", student)

      producer.send(record)
    }

    source.close()
    producer.close()
  }

}
